import Vue from 'vue'
import App from './App.vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import store from './store';
import router from './router';
import qfSubMenu from "qf-sub-menu"
import nprogress from 'nprogress' 
import 'nprogress/nprogress.css'

Vue.use(ElementUI);
Vue.use(qfSubMenu)

Vue.config.productionTip = false


router.beforeEach((to,from,next)=>{
  nprogress.start()
    let token = localStorage.getItem('crm-token') || ""
    if(token){
      // store中的userRoutes为空数组时，就要获取用户菜单
      if(store.state.userRoutes.length===0){
        store.dispatch('GET_MENULIST')//因为actions函数加了async，所以打印的是promise<pedding>状态
        .then(()=>{
          next({path:to.path})//如果直接next，动态路由还未生成
        })
      }else{
        next()
      }
      // next()
    }else{
      if(to.path!=='/login'){
        next({path: "/login"})
      }else{
        next()
      }
    }
})

//自定义键权指令
Vue.directive("has",{
  inserted(el,binding){
    let permission = localStorage.getItem('permissionButtons') || '[]'
    permission = JSON.parse(permission)
    if(!permission.includes(binding.value)){
        //保存按钮原始类名
        let ElclassName = el.className
        el.className = ElclassName + " " + "is-disabled"
        el.disabled = true
    }
  }
})

router.afterEach((to,from)=>{
  nprogress.done()
  let newMatched = [...to.matched]
  newMatched.splice(0,1)
  console.log(newMatched);
  store.commit('SET_BREADCRUMB',newMatched)
})


new Vue({
  store,
  router,
  render: h => h(App),
}).$mount('#app')